<template>
	<!--pages/company/company.ttml-->
	<form @submit="formSubmit" @reset="formReset">
		<view class="content">
			<view class="hr"></view>
			<view class="item">
				<input type="text" @input="usernametBlur" name="username" placeholder="请设置4-20位用户名" placeholder-class="holder"
				 bingblur="accountblur" />
			</view>
			<view class="item flex">
				<input type="text" @input="pwdBlur" password name="password" placeholder="请设置5-20位登录密码" placeholder-class="holder" />
				<!-- <switch type="switch" name="switch" /> -->
			</view>
			<!-- <view class="item">
     <input type="text" name="company" placeholder="请填写工商局注册名称" placeholder-class="holder" />
   </view> -->
			<view class="item">
				<input type="text" @input="nicknameBlur" name="nickname" placeholder="联系人姓名" placeholder-class="holder" />
			</view>
			<view class="mobileInfo">
				<view class="mobile">
					<input type="text" @input="phoneBlur" name="phone" placeholder="请输入手机号" placeholder-class="holder" />
				</view>
				<button class="code" v-show="sendAuthCode" @tap="sendCode">发送验证码</button>
				<span v-show="!sendAuthCode" class="auth_text" style="color:red;padding:9px;"><span class="auth_text_blue">{{auth_time}}</span>秒之重新发送验证码</span>
			</view>
			<view class="item">
				<input type="text" @input="codeBlur" name="code" placeholder="短信验证码" placeholder-class="holder" />
			</view>
			<button class="btn" :disabled="disabled" :type="btnstate" form-type="submit" @tap="register">注册</button>
			<view class="xieyi">
				<text class="agree">注册即视为同意</text><text class="opinion">中国用户注册协议</text>
			</view>
		</view>
	</form>
</template>

<script>
	let app = getApp();

	export default {
		data() {
			return {
				sendAuthCode: true,
				auth_time: '',
				disabled: true,
				btnstate: "default",
				username: "",
				password: "",
				nickname: "",
				phone: "",
				code: ""
			};
		},
		components: {},
		props: {},
		methods: {
			usernametBlur: function(e) {
				var username = e.detail.value;
				uni.setStorageSync('username', username);

				if (username != "") {
					this.setData({
						disabled: false,
						btnstate: "primary",
						username: username
					});
				}
			},
			pwdBlur: function(e) {
				var password = e.detail.value;
				uni.setStorageSync('password', password);

				if (password != "") {
					this.setData({
						password: password
					});
				}
			},
			nicknameBlur: function(e) {
				var nickname = e.detail.value;
				uni.setStorageSync('nickname', nickname);

				if (nickname != "") {
					this.setData({
						nickname: nickname
					});
				}
			},
			phoneBlur: function(e) {
				var phone = e.detail.value;
				console.log(phone)
				uni.setStorageSync('phone', phone);

				if (phone != "") {
					this.setData({
						phone: phone
					});
				}
			},
			codeBlur: function(e) {
				var code = e.detail.value;
				uni.setStorageSync('code', code);

				if (mobile != "") {
					this.setData({
						code: code
					});
				}
			},
			sendCode: function() {
				this.sendAuthCode = false;
				this.auth_time = 30;
				var auth_timetimer = setInterval(() => {
					this.auth_time--;
					if (this.auth_time <= 0) {
						this.sendAuthCode = true;
						clearInterval(auth_timetimer);
					}
				}, 1000);
				uni.request({
					url: `https://localhost:10000/user/sendCode`,
					// 目标服务器url
					method: 'GET',
					dataType: 'json',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						phone: uni.getStorageSync('phone')
					},
					success: res => {
						console.log(res.data)
						uni.showModal({
							title: '提示',
							content: res.data.message
						});
					}
				});
			},
			register: function() {
				console.log(uni.getStorageSync('username'))
				console.log(uni.getStorageSync('password'))
				console.log(uni.getStorageSync('nickname'))
				console.log(uni.getStorageSync('phone'))
				console.log(uni.getStorageSync('code'))
				uni.request({
					url: `${app.globalData.baseUrl}/user/addUser`,
					//url: `https://localhost:10000/user/addUser`,
					// 目标服务器url
					method: 'POST',
					dataType: 'json',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						username: uni.getStorageSync('username'),
						password: uni.getStorageSync('password'),
						nickname: uni.getStorageSync('nickname'),
						phone: uni.getStorageSync('phone'),
						code: uni.getStorageSync('code')
					},
					success: res => {
						console.log(res.data)
						let code = res.data.code;
						if (code === 200) {
							uni.reLaunch({
								url: `../login/login`
							})
						} else {
							uni.showModal({
								title: '提示',
								content: "验证码错误"
							});
						}
					},
				});
			}
		}
	};
</script>
<style>
	/* pages/company/company.ttss */
	.content {
		width: 100%;
		height: 700px;
		background-color: #f2f2f2;
	}

	.hr {
		padding-top: 40px;
	}

	.item {
		margin: 0 auto;
		border: 1px solid #cccccc;
		height: 40px;
		width: 90%;
		border-radius: 3px;
		background-color: #ffffff;
		margin-bottom: 15px;
	}

	.item input {
		height: 40px;
		line-height: 40px;
		margin-left: 10px;
	}

	.holder {
		font-size: 14px;
		color: #999999;
	}

	.flex {
		display: flex;
		flex-direction: row;
	}

	.flex input {
		width: 300px;
	}

	item switch {
		margin-top: 5px;
		margin-right: 5px;
	}

	.mobileInfo {
		display: flex;
		flex-direction: row;
	}

	.mobile {
		margin: 0 auto;
		border: 1px solid #cccccc;
		height: 40px;
		width: 50%;
		border-radius: 3px;
		background-color: #ffffff;
		margin-bottom: 15px;
		display: flex;
		flex-direction: row;
		margin-left: 5%;
	}

	.mobile input {
		margin-top: 8px;
		margin-left: 10px;
	}

	.code {
		border: 1px solid #cccccc;
		height: 40px;
		width: 35%;
		background-color: #edeeec;
		border-radius: 3px;
		text-align: center;
		margin-left: 10px;
		line-height: 40px;
		color: #999999;
		font-size: 15px;
		margin-bottom: 15px;
		margin-right: 5%;
	}

	.btn {
		width: 90%;
		color: #999999;
		margin-top: 40px;
	}

	.xieyi {
		margin-top: 15px;
		margin-left: 15px;
		font-size: 13px;
	}

	.agree {
		margin-left: 5px;
		color: #666666;
	}

	.opinion {
		color: red;
		font-weight: bold;
		text-decoration: underline;
	}
</style>
